﻿/*
 * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

#ifndef USBPACKET_H
#define USBPACKET_H

#include <windows.h>
#include <QVector>
struct Tlv {
    uint8_t type;
    uint16_t lenth;
    PBYTE data;
    Tlv()
        : type(0),
          lenth(0),
          data(nullptr)
    {
    }
};
struct PayloadBuffer {
    uint8_t serverId;
    uint8_t commandId;
    uint16_t bodyLen;
    uint8_t body[0];
    PayloadBuffer()
        : serverId(0),
          commandId(0),
          bodyLen(0) {}
    ~PayloadBuffer() {}
};

struct SleFrameHeader {
    uint8_t flag;
    uint8_t version;
    uint8_t totalFrame;
    uint8_t frameSeq;
    uint8_t rsv;
    SleFrameHeader()
        : flag(0x1f),
          version(0),
          totalFrame(1),
          frameSeq(0),
          rsv(0) {}
};

namespace UsbPacketClass {
class UsbPacket {
public:
    UsbPacket();
    ~UsbPacket();
    PBYTE GetBuffer();
    uint32_t GetBufferSize() const;
    void Init(PBYTE input, uint32_t inputLen);
    void SetData(const PBYTE pcData, uint16_t u16DataSize);
    void SetTlv(const uint8_t type, const PBYTE pcData, uint16_t u16DataSize);
    PBYTE GetPayloadPack(const uint8_t serviceId, const uint8_t commandId);
    uint32_t GetPayloadBufferSize() const;
    BOOL CheckCrc() const;
    PBYTE GetData();
    uint16_t GetDataSize() const;
    PayloadBuffer *GetReadPayloadBuffer();
    QVector<Tlv> GetTlvVec(PBYTE tlvByte, uint16_t tlvByteSize);
    uint32_t GerReadPayloadBufferSize() const;
    PBYTE GetBuffer(int total, int offset);
private:
    uint16_t dataSize;
    PBYTE data;
    PBYTE buffer;
    uint32_t bufferSize;
    PBYTE tlvData;
    uint16_t tlvDataSize;
    PBYTE payloadBuffer;
    uint32_t payloadBufferSize;
    uint8_t startFlag;
    uint16_t crc;
    PayloadBuffer *readPayloadBuffer;
    uint32_t readPayloadBufferSize;
    unsigned short checkCrc;
    QVector<Tlv> tlv;
};
}

#endif // USBPACKET_H
